package com.hrt.manage.address.province.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Area;
import com.hrt.bean.City;
import com.hrt.bean.Province;
import com.hrt.manage.address.area.service.AreaService;
import com.hrt.manage.address.city.service.CityService;
import com.hrt.manage.address.province.service.ProvinceService;

@Controller
@RequestMapping(value = "/manage/province")
public class ProvinceController extends BaseController<Province>{
	
	@Autowired ProvinceService provinceServince;
	@Autowired CityService cityServince;
	@Autowired AreaService areaService;
	/**
	 * 
	 * @param pageIndex
	 * 
	 * @param pageSize
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(provinceServince.totalCount());

		List<Province> resultList = provinceServince.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();

		list = provinceServince.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(provinceServince.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		view.addObject("pageResult", page);
		return view;

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		List<City> cityList=null;
		for(int i=0;i<idArr.length;i++){
			Province p=provinceServince.getProvinceById(idArr[i]);
			if(p!=null){
				 cityList=new ArrayList<City>();
			String provincecode = provinceServince.getProvinceById(idArr[i]).getCode();
			 cityList = cityServince.getByProvinceCode(provincecode);
			 
			}
			List<Long> areaIds= new ArrayList<Long>();
			List<Long> cityIds= new ArrayList<Long>();
			if(cityList!=null && !cityList.isEmpty()){
			for(int j=0;j<cityList.size();j++){
				City  city=cityList.get(j);
				String citycode = city.getCode();
				cityIds.add(city.getId());
				List<Area> areaList = areaService.getAreaByCityCode(citycode);
				
				
				
				for(int idx =0;idx<areaList.size();idx++){
					Long id = areaList.get(idx).getId();
					areaIds.add(id);
				}
			}
			}
			
			
			
			try {
				areaService.deleteById(areaIds.toArray(new Long[areaIds.size()]));
				cityServince.deleteById(cityIds.toArray(new Long[cityIds.size()]));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			
		}
		
		try {
			provinceServince.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("provinceInfor") Province province,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("save");
		try {
			provinceServince.saveProvince(province);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) Long provinceId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (provinceId != null && !"".equals(provinceId)) {
			Province province = this.provinceServince.getProvinceById(provinceId);
			if (province != null) {
				map = transBean2Map(province);
			}

		}
		return map;

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("provinceInfor") Province province,
			HttpServletRequest request, HttpServletResponse response) {
		provinceServince.updateProvince(province);
		try {
			response.sendRedirect("list.do");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
